import _IntlField from "choerodon-ui/pro/lib/intl-field";
import _Upload from "choerodon-ui/pro/lib/upload";
import _isNil from "lodash/isNil";
import React from 'react';
import { getEnvConfig } from "../../../../utils/iocUtils";
import { getCurrentOrganizationId, getUserOrganizationId } from "../../../../utils/utils";

var _getEnvConfig = getEnvConfig('config'),
    CPAAS_PLATFORM = _getEnvConfig.CPAAS_PLATFORM;

export function FlexLink(options) {
  var extra = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var _extra$currentData = extra.currentData,
      currentData = _extra$currentData === void 0 ? {} : _extra$currentData;
  var linkTitle = options.linkTitle,
      linkHref = options.linkHref,
      linkNewWindow = options.linkNewWindow;
  var newHref = linkHref || '';
  var newTitle = linkTitle || '';
  var mappings = newHref.match(/{([^{}]*)}/g);
  var titleMappings = newTitle.match(/{([^{}]*)}/g);

  if (mappings) {
    newHref = replace(mappings, currentData, newHref);
  }

  if (titleMappings) {
    newTitle = replace(titleMappings, currentData, newTitle);
  }

  return /*#__PURE__*/React.createElement("a", {
    href: newHref,
    target: linkNewWindow ? '_blank' : '_self',
    rel: "noopener noreferrer"
  }, newTitle);
}
export function FlexUpload(props) {
  return /*#__PURE__*/React.createElement(_Upload, props);
}
export function FlexIntlField(props) {
  var tlsUrl = "".concat(CPAAS_PLATFORM, "/v1/multi-language");
  return /*#__PURE__*/React.createElement(_IntlField, Object.assign({
    tlsUrl: tlsUrl
  }, props));
}

function replace(mappings, values, targetString) {
  var newString = targetString;

  for (var i = 0; i < mappings.length; i++) {
    if (mappings[i] === '{organizationId}' || mappings[i] === '{tenantId}') {
      // eslint-disable-next-line no-continue
      continue;
    }

    var key = mappings[i].match(/{([^{}]*)}/)[1];
    var value = _isNil(values[key]) ? '' : values[key];
    newString = newString.replace("{".concat(key, "}"), value);
  }

  newString = newString.replace(/{organizationId}/, getCurrentOrganizationId());
  newString = newString.replace(/{tenantId}/, getUserOrganizationId());
  return newString;
}